package com.sacc.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.power.common.model.CommonResult;
import com.sacc.mapper.BoxAllMapper;
import com.sacc.pojo.BoxAll;
import com.sacc.pojo.BoxCheck;
import com.sacc.service.BoxAllService;
import com.sacc.service.BoxInfoService;
import com.sacc.service.BoxProductService;
import com.sacc.util.FileUtil;
import com.sacc.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 滚~韬
 * @date 2022/4/12 13:28
 */
@Service
public class BoxAllServiceImpl implements BoxAllService {
    @Autowired
    private BoxAllMapper boxAllMapper;
    @Autowired
    private BoxInfoService boxInfoService;
    @Autowired
    private BoxProductService boxProductService;
//    @Autowired
//    private FileUtil fileUtil;
    /**
     * 根据boxId去获取蜂箱所有展示信息
     * @param boxId
     * @return
     * @throws IOException
     */
    public BoxAll getBox(Integer boxId) throws IOException {
//        BoxCheck boxCheck = new BoxCheck();
        BoxAll boxAll = boxAllMapper.selectOne(new QueryWrapper<BoxAll>().eq("box_id",boxId));//获取蜂箱的box_all
//        boxCheck.setBoxId(boxId);
//        boxCheck.setBoxDesc(boxAll.getBoxDesc());
//        boxCheck.setBoxName(boxAll.getBoxName());
//        boxCheck.setBoxInfo(boxInfoService.Get(boxId));
//        boxCheck.setState(boxAll.getState());
//        boxCheck.setProductInfoAlls(boxProductService.getByBoxId(boxId));
//        boxCheck.setImage(FileUtil.getFile(boxAll.getBoxImageUrl()));
        return  boxAll;
    }

    /**
     * 根据boxId获取部分蜂箱展示信息
     * @param boxId
     * @return
     * @throws IOException
     */
    public BoxCheck getPartofBox(Integer boxId) throws IOException {
        BoxCheck boxCheck = new BoxCheck();
        BoxAll boxAll = boxAllMapper.selectOne(new QueryWrapper<BoxAll>().eq("box_id",boxId));//获取蜂箱的box_all
        boxCheck.setBoxId(boxId);
        boxCheck.setBoxName(boxAll.getBoxName());
        boxCheck.setState(boxAll.getState());
        boxCheck.setImage(FileUtil.getFile(boxAll.getBoxImageUrl()));
        return  boxCheck;
    }

    /**
     * 根据蜂箱id List获取BoxCheck群
     * @return
     */
    public List<BoxCheck> getBoxAllsById(List<Integer> boxId){
        if(boxId.isEmpty()){
            return Collections.EMPTY_LIST;
        }
        List<BoxAll> boxAlls = new ArrayList<>();
        boxId.forEach(b->{
            boxAlls.add(boxAllMapper.selectOne(new QueryWrapper<BoxAll>().eq("box_id", b)));
        });
        List<BoxCheck> boxChecks = new ArrayList<>();
        boxAlls.forEach(b->{
            BoxCheck boxCheck = new BoxCheck();
            boxCheck.setBoxDesc(b.getBoxDesc());
            boxCheck.setBoxId(b.getBoxId());
            boxCheck.setBoxName(b.getBoxName());
            boxCheck.setState(b.getState());
            boxCheck.setImage(b.getBoxImageUrl());
            boxChecks.add(boxCheck);
        });
        return boxChecks;
    }
    /**
     * 反馈全部蜂箱
     * @return
     */
    public List<BoxAll> getAll(){
        List<BoxAll> boxAlls = boxAllMapper.selectList(null);
        return  boxAlls;
    }

    /**
     * 添加一个box_all
     * @param boxAll
     * @return
     */
    public CommonResult addOne(BoxAll boxAll, MultipartFile[] files){
        try{
//          String imageUrl = fileUtil.upload("/boxlist/",file,"image");
            CommonResult upload = OssUtil.Uploads(files);
            boxAll.setCreateTime(LocalDateTime.now());
            boxAll.setBoxImageUrl((String) upload.getData());
            boxAllMapper.insert(boxAll);
            return upload;
        }catch (Exception e){
            return new CommonResult("400","添加失败"+e.toString());
        }
    }
}
